import { BattleData } from "../Data/BattleData";
import { GameUI } from "../UI/gameUI";
import { MenuUI } from "../UI/menu";
import { StageSelectUI } from "../UI/stageSelectUI";
import DataManager from "../utils/DataSystem/DataManager";
import { StateBlock } from "../utils/StateBlock/StateBlock";
import UIManager from "../utils/UIFramework/UIManager";

/**
 * App状态盒
 */
export default class AppStateBlock extends StateBlock{
    protected InitState: typeof StateBlock = AppStateBlock_MainMenu;

    public currentBattle : BattleData = null;
}

class AppStateBlock_MainMenu extends StateBlock{
    public async onEnter() {
        let menu = await UIManager.Instance.OpenUI<MenuUI>("menu",UIManager.UI_LAYER.UIPage);
        menu.StartGame.AddFunc(this._onStartGame,this.parent);
    }

    _onStartGame(self : AppStateBlock) {
        self.changeState(AppStateBlock_Loading);
    }

    public onExit(): void {
        UIManager.Instance.CloseUI("menu");
    }
}

class AppStateBlock_Loading extends StateBlock{
    public async onEnter(): Promise<void> {
        //  总之在loading
        await DataManager.Instance.preloadData();
        DataManager.Instance.TestShowData();
        //  之后进入game
        this.parent.changeState(AppStateBlock_StateSelect);
    }
}

class AppStateBlock_StateSelect extends StateBlock{
    public async onEnter() {
        let ui = await UIManager.Instance.OpenUI("stageSelect",UIManager.UI_LAYER.UIPage) as StageSelectUI;
        ui.StartBattle.AddFunc(this._onStartBattle,this.parent);
    }

    private _onStartBattle(self: AppStateBlock, param: any[]) {
        let battle = param[0] as BattleData;
        self.currentBattle = battle;
        self.changeState(AppStateBlock_Game);
    }

    public onExit(): void {
        UIManager.Instance.CloseUI("stageSelect");
    }

    
}

class AppStateBlock_Game extends StateBlock{
    public async onEnter(): Promise<void> {
        let game = await UIManager.Instance.OpenUI("game",UIManager.UI_LAYER.UIPage) as GameUI;
        let app = this.parent as AppStateBlock;
        game.ReturnMenu.AddFunc(this._returnStageSelect,this.parent)
        game.BattleStart(app.currentBattle);
    }

    public onExit(): void {
        UIManager.Instance.CloseUI("game");
    }

    private _returnStageSelect(self : AppStateBlock)
    {
        self.changeState(AppStateBlock_StateSelect);
    }
}